﻿


<div class="@Class">
    <SectionTitleCard Title="自定义链接" />
    <CnGalWebSite.Components.Buttons.MasaButton Rounded Text="添加" OnClick="AddOther" Icon="@IconType.Create.ToIconString()" Class="mb-4" />

    @if (Model.Any(s => Check(s.DisplayName)))
    {
            <MSimpleTable Class="mb-4">
        <thead>
            <tr>
                <th class="text-left">
                    唯一名称
                </th>
                <th class="text-left">
                    链接
                </th>
                <th class="text-left">
                    简介
                </th>
                <th class="text-left">
                    操作
                </th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model.Where(s => Check(s.DisplayName)))
            {
                <tr @key="item.DisplayName+item.DisPlayValue+item.Link">
                    <td style="min-width: 50px; ">@item.DisplayName</td>
                    <td style="overflow-wrap: anywhere; min-width: 200px; ">@item.Link</td>
                    <td style="overflow-wrap: anywhere; min-width: 200px; ">@item.DisPlayValue</td>
                    <td>
                        <CnGalWebSite.Components.Buttons.MasaButton Tooltip="编辑" OnClick="@(()=>EditOther(item))" Icon="@IconType.Edit.ToIconString()" Color="success" />
                        <CnGalWebSite.Components.Buttons.MasaButton Tooltip="删除" OnClick="@(()=>DeleteOther(item))" Icon="@IconType.Delete.ToIconString()" Color="error" />
                    </td>
                </tr>
            }
        </tbody>
    </MSimpleTable>

    }
</div>
@if (_model != null)
{
    <MDialog @bind-Value="_editDialog"
             MaxWidth="800">
        <MDCard>
            <MCardTitle>
                编辑外部链接
            </MCardTitle>
            <MCardText>
                <MRow>
                    <MCol Cols="12" Md="6">
                        <MTextField @bind-Value="_model.DisplayName" Label="唯一名称" />
                    </MCol>
                    <MCol Cols="12" Md="6">
                        <MTextField @bind-Value="_model.Link" Label="链接" />
                    </MCol>
                    <MCol Cols="12">
                        <MTextField @bind-Value="_model.DisPlayValue" Label="简介" />
                    </MCol>
                </MRow>

            </MCardText>
        </MDCard>
    </MDialog>
}
@code {
    [Parameter]
    public List<RelevancesModel> Model { get; set; }
    [Parameter]
    public string Title { get; set; }
    [Parameter]
    public string Class { get; set; }

    private bool _editDialog;
    RelevancesModel _model;

    public bool Check(string name)
    {
        if(string.IsNullOrWhiteSpace(name))
        {
            return true;
        }
        var items = Enum.GetValues(typeof(OutlinkType));
        foreach(var item in Enum.GetValues(typeof(OutlinkType)))
        {
            if(name.ToLower()== ((OutlinkType)item).GetDisplayName().ToLower())
            {
                return false;
            }
        }
        return true;
    }

    public void DeleteOther(RelevancesModel item)
    {
        Model.Remove(item);
        StateHasChanged();
    }
    public void EditOther(RelevancesModel item)
    {
        _model=item;
        _editDialog=true;
        StateHasChanged();
    }

    public void AddOther()
    {
        _model = new RelevancesModel();
        Model.Add(_model);
        _editDialog = true;
        StateHasChanged();
    }
}
